package org.ruanjj.io;

import org.junit.Test;

import java.io.*;

public class ObjectOutputInputStreamMain {
    @Test
    public void test(){
        //序列化
        File a = new File("fatherObject.txt");
        try(
               FileOutputStream fo =  new FileOutputStream(a);
               ObjectOutputStream oo =  new ObjectOutputStream(fo)
          ){
            Father1 f = new Father1("Tony",12);
            oo.writeObject(f);
            System.out.println("序列化成功");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //反序列化
        try(
                FileInputStream fi = new FileInputStream(a);
                ObjectInputStream oi = new ObjectInputStream(fi);
                ){
            Father1 f = (Father1)oi.readObject();
            System.out.println(f.getName());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
